<template>
  <div class="content">
    <div class="title">
      <h3>合同信息</h3>
      <Button type="primary" v-if="unlocking('0102010801')" @click='onCreate'>新建合同</Button>
    </div>
    <!-- <dt-grid ref="table" :list='list' height-resize /> -->
    <dt-grid ref="table" :list='list'/>
    
    <div class="title" style="margin: 30px 0 10px;">
      <h3>撤场信息</h3>
      <Button type="primary" v-if="unlocking('0102010803')" @click='onEditInfo'>编辑</Button>
    </div>
    <dt-info :line-number='2' :label-width="140" ref='info'></dt-info>
    
    <contract-modal ref='contract' @success='detail' @abolish='abolish' />
    <info-modal ref='withdrawal' @success='detail' />
  </div>
</template>

<script>
  import Column from './opt/Column.js'
  import DetailOpt from './opt/DetailOpt.js'
  
  import contractModal from './components/contract-modal.vue'
  import infoModal from './components/info-modal.vue'
  
  import contractMgr from '@/biz/buildingManagement/contract.js'
  
  export default {
    components: {
      contractModal,
      infoModal
    },
    data() {
      return {
        list: [],
        info: {}
      }
    },
    mounted() {
      this.$refs.table.init(this, new Column())
      this.detail()
    },
    methods: {
      abolish(){
              this.detail()
      },
      detail() {
        contractMgr.detail().then(res => {
          this.info = res
          this.$refs.info.init(new DetailOpt(this.info), this.info)
          this.list = res.contractInformList.map(i => ({
            ...i,
            endType: !isNaN(Date.parse(i.contractEndTime)) ? '1' : '2'
          }))
        })
      },
      onCreate() {
        this.$refs.contract.create({
          prev: this.list.length > 0 ? this.list[this.list.length - 1] : null,
          next: null
        })
      },
      onEditContract(item) {
        console.log(item,'bbb');
        item.attachList&&item.attachList.forEach(element => {
          if(!element.name){
            element.name = element.imageName
            element.url = element.imageURL
          }
          if(!element.imageName){
            element.imageName = element.name
            element.imageURL = element.url
          }          
        });
        let idx = this.list.findIndex(i => i.contractInformId == item.contractInformId)
        this.$refs.contract.edit({
          info: item,
          prev: idx == 0 ? null : this.list[idx - 1],
          next: idx == this.list.length - 1 ? null : this.list[idx + 1]
        })
      },
      onEditInfo() {
        this.$refs.withdrawal.show(this.info)
      },
    },
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 0 20px;
  }
  
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
